/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl.actor;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import com.cxhd.lis.DataCache;
import com.cxhd.lis.model.newmodel.LisResult;
import com.cxhd.lis.util.HttpClientUt;

import com.google.common.io.Files;
import com.google.gson.Gson;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author Administrator
 */
public class SendInfoToCenterActor extends UntypedActor {

    private Record info = null;
    private List<Record> result = null;
    private String lis_samplecollection = null;

    @Override
    public void onReceive(Object o) throws Exception {

        try {
             lis_samplecollection = (String) o;
            getPatInfo(lis_samplecollection);
            sendData();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sendFinish();
        }
    }

    public void getPatInfo(String sample_info_id) {

        String sql = " select * from lis_samplecollection a left join lis_patient b on a.INFOPK = b.PK WHERE a.PK=" + sample_info_id;
        // System.out.println(sql);
        info = Db.findFirst(sql);
    }

    public void sendData() {

        if (DataCache.getIntance().getConfig().get("lisCenterUrl") != null && DataCache.getIntance().getConfig().get("lisCenterUrl").length() > 5) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("lisinfo", JsonKit.toJson(info)));
            //params.add(new BasicNameValuePair("result", JsonKit.toJson(result)));
            params.add(new BasicNameValuePair("hospital_code", DataCache.getIntance().getLocalhostSN()));

            String returnData = HttpClientUt.httpPost("http://" + DataCache.getIntance().getConfig().get("lisCenterUrl") + "/lisPage/otherInfo/recviceLisInfo", params);
            if (returnData.equalsIgnoreCase("true")) {
                sendSuccess();
            }
        }
    }

    public void sendSuccess() {
        System.out.println("update lis_samplecollection set SEND_CENTER_STATUS = 1 where PK=" + lis_samplecollection);
        Db.update("update lis_samplecollection set SEND_CENTER_STATUS = 1 where PK=" + lis_samplecollection);

    }

    public void sendFinish() {
        lis_samplecollection = null;
        info = null;
      
    }

}
